package com.baitiaojun.common.utils.servlet;


import cn.hutool.http.ContentType;
import com.alibaba.fastjson.JSONObject;
import com.baitiaojun.common.constants.HttpStatus;
import com.baitiaojun.common.utils.convert.ParamConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * servlet工具
 */
public class ServletUtils {

    public static String getStrParam(String paramName) {
        return ParamConvertUtils.convertStr(getParam(paramName));
    }

    public static Long getLongParam(String paramName) {
        return ParamConvertUtils.convertLong(getParam(paramName));
    }

    public static Integer getIntParam(String paramName) {
        return ParamConvertUtils.convertInteger(getParam(paramName));
    }

    public static Boolean getBoolParam(String paramName) {
        return ParamConvertUtils.convertBoolean(getParam(paramName));
    }

    /**
     * 获取参数
     * @param paramName
     * @return
     */
    public static Object getParam(String paramName) {
        return getHttpServletRequest().getRequest().getAttribute(paramName);
    }

    /**
     * 获取json型参数
     * @param clazz
     * @param defaultValue
     * @return
     * @param <T>
     */
    public static <T> T getJsonParam(Class<T> clazz, T defaultValue) {
        try {
            BufferedReader reader = getRequest().getReader();
            return ParamConvertUtils.convertToObject(reader, clazz, defaultValue);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    public static HttpServletRequest getRequest() {
        return getHttpServletRequest().getRequest();
    }

    public static HttpServletResponse getResponse() {
        return  getHttpServletRequest().getResponse();
    }

    public static ServletRequestAttributes getHttpServletRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) requestAttributes;
    }

    public static void render(HttpServletResponse response, Object target) {
        render(response, JSONObject.toJSONString(target));
    }

    /**
     * 异步返回json
     * @param response
     * @param content
     */
    public static void render(HttpServletResponse response, String content) {
        response.setContentType(ContentType.JSON.getValue());
        response.setStatus(HttpStatus.SUCCESS);
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        try {
            response.getWriter().write(content);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean isAsync(HttpServletRequest request) {
        String accept = request.getHeader("accept");
        if (accept != null && accept.contains(ContentType.JSON.getValue())) {
            return true;
        }
        String XRequestedWith = request.getHeader("X-Requested-With");
        if (XRequestedWith != null && XRequestedWith.contains("XMLHttpRequest")) {
            return true;
        }
        String uri = request.getRequestURI();
        if (StringUtils.equalsAny(uri, ".json", ".xml")) {
            return true;
        }
        String ajax = request.getParameter("__ajax");
        return StringUtils.equalsAny(ajax, "json", "xml");
    }
}
